<?php
/**
 * Created by PhpStorm.
 * User: sosyuki
 * Date: 2018/8/24
 * Time: 15:34
 */

namespace Drupal\points\ViewsController\handler\field;


use stdClass;
use views_handler_field_user_link;

class EditBalanceLink extends views_handler_field_user_link {

  function access() {
    return user_access('administer points');
  }

  public function option_definition() {
    $options = parent::option_definition();
    $types = points_get_types();
    $type = reset($types);
    $options['type'] = ['default' => $type->type];
    return $options;
  }

  public function options_form(&$form, &$form_state) {
    $types = points_get_types();
    $options = [];
    foreach ($types as $type) {
      $options[$type->type] = $type->label;
    }
    $form['type'] = [
      '#type' => 'select',
      '#title' => t('类型'),
      '#options' => $options,
      '#default_value' => $this->options['type'],
    ];
    parent::options_form($form, $form_state);
  }

  function render_link($data, $values) {
    // Build a pseudo account object to be able to check the access.
    $account = new stdClass();
    $account->uid = $data;
    if ($data && user_access('administer points')) {
      $this->options['alter']['make_link'] = TRUE;

      $text = !empty($this->options['text']) ? $this->options['text'] : t('增加余额');

      $points_type = $this->options['type'];

      $this->options['alter']['path'] = "admin/content/points/{$data}/{$points_type}";
      $this->options['alter']['query'] = drupal_get_destination();

      return $text;
    }
  }
}
